#include <gtk/gtk.h>


/* This is a callback function. The data arguments are ignored
 *  * in this example. More on callbacks below. */
static void hello( GtkWidget *widget,
                   gpointer   data )
{
    //useing g_print to print string on console
    g_print ("Hello World\n");
}

static gboolean delete_event( GtkWidget *widget,
                              GdkEvent  *event,
                              gpointer   data )
{
/* If you return FALSE in the "delete_event" signal handler,
 * GTK will emit the "destroy" signal. Returning TRUE means
 * you don't want the window to be destroyed.
 * This is useful for popping up 'are you sure you want to quit?'
 * type dialogs. */

    //useing g_print to print string on console
    g_print ("delete event occurred\n");

    /* Change TRUE to FALSE and the main window will be destroyed with
 *      * a "delete_event". */

    return FALSE;//TRUE;
}

/* Another callback */
static void destroy( GtkWidget *widget,
                     gpointer   data )
{
    gtk_main_quit ();
}

int main(int   argc, char *argv[])
{
    GtkWidget *window;
    GtkWidget *button;
    
    gtk_init(&argc, &argv);
    
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

    g_signal_connect(G_OBJECT (window), 
		"delete_event",
		G_CALLBACK (delete_event), 
		NULL);
    
    g_signal_connect(G_OBJECT (window), 
		"destroy",
		G_CALLBACK (destroy), 
		NULL);

    button = gtk_button_new_with_label ("Hello World");

    gtk_container_set_border_width (GTK_CONTAINER (window), 10);
 
    g_signal_connect(
		G_OBJECT (button), 
		"clicked",
		G_CALLBACK (hello), 
		NULL);

    /*
	//useing the function bellow to register button's click call back,
 	//when gtk_widget_destroy called, the first argument is not button but window
 	//gtk_widget_destroy used to destroy window obj, getting 'window' as the first argument
 	//so as soon as the button clicked the whole window shut dnow
    g_signal_connect_swapped(G_OBJECT (button), "clicked",
			      G_CALLBACK (gtk_widget_destroy),
                              G_OBJECT (window));
    //*/
    gtk_container_add(GTK_CONTAINER (window), button);

    gtk_widget_show(button);
       
    gtk_widget_show(window);
    
    gtk_main ();
    
    return 0;
}
